# -*- coding: utf-8 -*-

import pygame,time,sys
from spites_util import *

myColorScheme = ColorScheme()

class Main():
    def __init__(self):
        # the screen as started by core
        self.screen = pygame.display.get_surface()

        # access to the sound channels
        self.bgm = pygame.mixer.Channel(0)
        self.bgs = pygame.mixer.Channel(1)
        self.mxf = pygame.mixer.Channel(2)
        self.sxf = pygame.mixer.Channel(3)
        self.bgm.play(intro)

        # are we done?
        self.not_done = True

        # the menu
        self.menu = ['1 player','2 player','Options','Exit']
        self.selection = 0

        # the keys
        self.up = False
        self.down = False

        # will be used later to set the update speed
        self.clock = pygame.time.Clock()
        self.tick = 60

    def get_input(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                self.tick = 10
                if event.key == pygame.K_ESCAPE:
                    sys.exit()
                if event.key == pygame.K_UP:
                    self.up = True   
                    #self.selection = (self.selection-1)%len(self.menu)
                    #self.sxf.play(point)
                if event.key == pygame.K_DOWN:
                    self.down = True
                    #self.selection = (self.selection+1)%len(self.menu)
                    #self.sxf.play(point) 
                if event.key == pygame.K_RETURN:
                    self.not_done = False
            if event.type == pygame.KEYUP:
                self.tick = 60
                if event.key == pygame.K_UP:
                    self.up = False
                if event.key == pygame.K_DOWN:
                    self.down = False

        if self.up:
            self.selection = (self.selection-1)%len(self.menu)
            self.sxf.play(point)
        if self.down:
            self.selection = (self.selection+1)%len(self.menu)
            self.sxf.play(point)    

    def draw(self):
        self.screen.fill(myColorScheme.white)
        gs = pygame.font.Font('gun4fsi.ttf', 72)
        out = gs.render('SPITES',1,pygame.Color('black'))
        w,h = out.get_size()
        self.screen.blit(out,[WIDTH//2-w//2,HEIGHT*1//5])
        gs = (pygame.font.Font('gun4fi.ttf', 32),
              pygame.font.Font('gun4fsi.ttf', 32))
        for text in self.menu:
            i = self.menu.index(text)
            color = 'black'
            out = gs[0].render(text,1,pygame.Color('black'))
            w,h = out.get_size()
            self.screen.blit(out,[WIDTH//2-w//2,HEIGHT*3//5+i*40])
            if i == self.selection:
                out = gs[1].render(text,1,myColorScheme.red)
                self.screen.blit(out,[WIDTH//2-w//2,HEIGHT*3//5+i*40])

    def loop(self):
        self.clock.tick(self.tick)
        self.get_input()
        self.draw()
        pygame.display.flip()
        return self.not_done
